339D - Xenia and Bit Operations - CodeForces Solution


data structures trees *1700

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <vector>

using namespace std;

vector <long long> a;
vector <long long> t;

void build(int v, int l, int r) {
    if (r - l == 1) {
        t[v] = a[l];
        return;
    }
    int m = (l + r) / 2;
    build(2 * v, l, m);
    build(2 * v + 1, m, r);
    if (__lg(r - l) % 2 == 1) {
        t[v] = t[2 * v] | t[2 * v + 1];
    } else {
        t[v] = t[2 * v] ^ t[2 * v + 1];
    }
}


void upd(int v, int l, int r, int i, long long x) {
    if (r - l == 1) {
        t[v] = x;
        return;
    }
    int m = (l + r) / 2;
    if (i < m) {
        upd(2 * v, l, m, i, x);
    } else {
        upd(2 * v + 1, m, r, i, x);
    }
    if (__lg(r - l) % 2 == 1) {
        t[v] = t[2 * v] | t[2 * v + 1];
    } else {
        t[v] = t[2 * v] ^ t[2 * v + 1];
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    a.resize(1 << n);
    t.resize(1 << (n + 2));
    for (int i = 0; i < (1 << n); ++i) {
        cin >> a[i];
    }
    build(1, 0, (1 << n));
    for (int i = 0; i < m; ++i) {
        int p, b;
        cin >> p >> b;
        upd(1, 0, (1 << n), p - 1, b);
        cout << t[1] << "\n";
    }
}

/* Tue Jan 31 2023 11:46:58 GMT+0300 (Moscow Standard Time) */


Comments

Submit
0 Comments
More Questions

1133C - Balanced Team
1704A - Two 0-1 Sequences
1467A - Wizard of Orz
1714E - Add Modulo 10
1714A - Everyone Loves to Sleep
764A - Taymyr is calling you
1714B - Remove Prefix
1264F - Beautiful Fibonacci Problem
52A - 123-sequence
1543A - Exciting Bets
1714D - Color with Occurrences
215B - Olympic Medal
1445A - Array Rearrangment
1351A - A+B (Trial Problem)
935B - Fafa and the Gates
1291A - Even But Not Even
1269A - Equation
441A - Valera and Antique Items
1702C - Train and Queries
816B - Karen and Coffee
838D - Airplane Arrangements
148B - Escape
847G - University Classes
1110A - Parity
1215B - The Number of Products
604C - Alternative Thinking
1204C - Anna Svyatoslav and Maps
322A - Ciel and Dancing
1689B - Mystic Permutation
1711B - Party